package com.woniuxy.hrm.consumer;

import com.rabbitmq.client.Channel;
import com.woniuxy.hrm.entity.Order;
import com.woniuxy.hrm.mapper.OrderMapper;
import com.woniuxy.hrm.util.RabbitmqConst;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Component
public class CheckOrderStatusConsumer {
    @Autowired
    private OrderMapper orderMapper;
    @RabbitListener(queues = RabbitmqConst.CHECK_ORDER_STATUS_QUEUE)
    public void receive(String msg, Channel channel, Message message) {
        try {
            Order order = orderMapper.queryOrderByOrderNum(msg);
            if (order.getStatus().equals(1)){
                //status==1  取消订单=3
                orderMapper.updateOrderStatus(order);
            }
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        } catch (Exception e) {
            try {
                channel.basicNack(message.getMessageProperties().getDeliveryTag(),false,false );
            } catch (IOException ex) {
                throw new RuntimeException(ex);
            }
            throw new RuntimeException(e);
        }
    }
}
